import Vue from "vue";
import Router from "vue-router";
import Login from "./views/Login.vue";
import Home from "./views/Home.vue";
import Welcome from "./views/Welcome.vue";
import User from "./views/users/User.vue";
import Rights from "./views/auth/Rights.vue";
import Roles from "./views/auth/Roles.vue";

Vue.use(Router);

var router = new Router({
  routes: [
    {
      path: "/login",
      component: Login
    },
    {
      path: "/home",
      component: Home,
      redirect: "/welcome",

      children: [
        { path: "/users", component: User },
        { path: "/welcome", component: Welcome },
        { path: "/rights", component: Rights },
        { path: "/roles", component: Roles }
      ]
    }
  ]
});

//路由守卫设置
// 在守卫中对token进行检测
router.beforeEach(function(to, from, next) {
  // 当前用户没有登录系统，并且还在访问非登录路由，就强制执行登录路由
  //用户没有登录系统（token===null）
  //该用户还要试图访问除登录页面以为的其他页面（例如：/home)
  //就强制该用户去登陆
  var token = window.sessionStorage.getItem("token");

  if (token === null && to.path !== "/login") {
    return next("/login");
  }
  next(); // 请继续你的旅行（放行）
});

export default router;
